package com.casic.ops.entity.vo;


import com.casic.ops.consts.app.ActionStatus;
import com.casic.ops.entity.domain.SchedulerTaskMachineRecordDO;
import com.casic.ops.utils.Utils;
import com.orion.utils.convert.TypeStore;
import com.orion.utils.time.Dates;
import lombok.Data;

import java.util.Date;
import java.util.Optional;

/**
 * @ClassName ops-platform
 * @Description  调度机器执行状态
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Data
public class SchedulerTaskMachineRecordStatusVO {

    /**
     * id
     */
    private Long id;

    /**
     * 执行记录id
     */
    private Long recordId;

    /**
     * @see ActionStatus
     */
    private Integer status;

    /**
     * 执行开始时间
     */
    private Date startTime;

    /**
     * 执行开始时间
     */
    private String startTimeAgo;

    /**
     * 执行结束时间
     */
    private Date endTime;

    /**
     * 执行结束时间
     */
    private String endTimeAgo;

    /**
     * 使用时间 ms
     */
    private Long used;

    /**
     * 使用时间
     */
    private String keepTime;

    /**
     * exitCode
     */
    private Integer exitCode;

    public static SchedulerTaskMachineRecordStatusVO recordDO2recordStatusVO(SchedulerTaskMachineRecordDO p) {
        SchedulerTaskMachineRecordStatusVO vo = new SchedulerTaskMachineRecordStatusVO();
        vo.setId(p.getId());
        vo.setRecordId(p.getRecordId());
        vo.setStatus(p.getExecStatus());
        vo.setExitCode(p.getExitCode());
        Date startTime = p.getStartTime();
        Date endTime = p.getEndTime();
        vo.setStartTime(startTime);
        vo.setEndTime(endTime);
        if (startTime != null && endTime != null) {
            vo.setUsed(endTime.getTime() - startTime.getTime());
            vo.setKeepTime(Utils.interval(vo.getUsed()));
        }
        Optional.ofNullable(startTime)
                .map(Dates::ago)
                .ifPresent(vo::setStartTimeAgo);
        Optional.ofNullable(endTime)
                .map(Dates::ago)
                .ifPresent(vo::setEndTimeAgo);
        return vo;
    }

    static {
        TypeStore.STORE.register(SchedulerTaskMachineRecordDO.class, SchedulerTaskMachineRecordStatusVO.class, p -> {
            SchedulerTaskMachineRecordStatusVO vo = new SchedulerTaskMachineRecordStatusVO();
            vo.setId(p.getId());
            vo.setRecordId(p.getRecordId());
            vo.setStatus(p.getExecStatus());
            vo.setExitCode(p.getExitCode());
            Date startTime = p.getStartTime();
            Date endTime = p.getEndTime();
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
            if (startTime != null && endTime != null) {
                vo.setUsed(endTime.getTime() - startTime.getTime());
                vo.setKeepTime(Utils.interval(vo.getUsed()));
            }
            Optional.ofNullable(startTime)
                    .map(Dates::ago)
                    .ifPresent(vo::setStartTimeAgo);
            Optional.ofNullable(endTime)
                    .map(Dates::ago)
                    .ifPresent(vo::setEndTimeAgo);
            return vo;
        });
    }
}
